﻿using AutoMapper;
using B.S.DataBase.MyProject.API.Appicant.Command;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command.RoleCommand;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.RoleCommandHandler
{
    public class OnenessRoleNameCommandHandler : IRequestHandler<OnenessRoleNameCommand, APIResult<object>>
    {
        private readonly IBaseRepository<Role> userrReppos;
        private readonly IMapper mapper;

        public OnenessRoleNameCommandHandler(IBaseRepository<Role> userrReppos, IMapper mapper)
        {
            this.userrReppos = userrReppos;
            this.mapper = mapper;
        }

        public Task<APIResult<object>> Handle(OnenessRoleNameCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var user = userrReppos.GetAll().Result.Where(x => x.RoleName == request.RoleName).ToList();
            if (user.Count() > 0)
            {
                result.Code = APIEnum.用户名重复;
                result.Msg = APIEnum.用户名重复.ToString();
            }
            else
            {
                result.Code = APIEnum.用户名唯一通过;
                result.Msg = APIEnum.用户名唯一通过.ToString();
            }
            return Task.FromResult(result);
        }
    }
}
